package com.lonty.intelligence.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author 陈璇
 * @Date 2020/4/2 17:06
 * @Version 1.0
 **/
@Component
public class RedisUtil {

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate template;
    private static RedisTemplate redisTemplate;

    public static final String ZSET = "zset";

    /**
     * 默认过期时长，单位：秒
     */
//    public static final long DEFAULT_EXPIRE = 5 * 60 * 1000;
    public static final long DEFAULT_EXPIRE = 600L;

    @PostConstruct
    public void init(){
        redisTemplate = this.template;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public static void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public static void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public static void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public static boolean exists(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public static Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存,带过期时间
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * List集合写入缓存
     *
     */
    public static boolean setList(final String key, Object object) {
        boolean result = false;
        try {
            redisTemplate.opsForList().rightPush(key,object);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * List集合写入缓存,带过期时间
     */
    public static boolean setList(final String key, Object object, Long expireTime) {
        boolean result = false;
        try {
            redisTemplate.opsForList().rightPush(key,object);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *List集合整个写入缓存
     */
    public static boolean setListAll(final String key, List list){
        boolean result = false;
        try {
            redisTemplate.opsForList().rightPushAll(key,list);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     *List集合整个写入缓存,带过期时间
     */
    public static boolean setListAll(final String key,List list,Long expireTime){
        boolean result = false;
        try {
            redisTemplate.opsForList().rightPushAll(key,list);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取List集合
     */
    public static List getList(final String key,long start,long end) {
        List result = null;
        result = redisTemplate.opsForList().range(key,start,end);
        return result;
    }

    /**
     * Set集合写入缓存
     */
    public static boolean setSet(final String key, Set set) {
        boolean result = false;
        try {
            SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
            for (Object o : set) {
                setOperations.add(key, o);
            }
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean setSet(final String key, Object object) {
        boolean result = false;
        try {
            SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
            setOperations.add(key,object);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Set集合写入缓存,带过期时间
     */
    public static boolean setSet(final String key,Object object, Long expireTime) {
        boolean result = false;
        try {
            SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
            setOperations.add(key, object);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean setSet(final String key, Set set, Long expireTime) {
        boolean result = false;
        try {
            SetOperations<String, Object> setOperations = redisTemplate.opsForSet();
            for (Object o : set) {
                setOperations.add(key, o);
            }
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取Set集合
     */
    public static Set getSet(final String key) {
        Set result = null;
        result = redisTemplate.opsForSet().members(key);
        return result;
    }

    /**
     * ZSet集合写入缓存
     */
    public static boolean setZSet(final String key, Object object, double score) {
        boolean result = false;
        try {
            ZSetOperations<String, Object> zsetOperations = redisTemplate.opsForZSet();
            zsetOperations.add(key,object,score);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * ZSet获取对应的score
     */
    public static Double score(String key, String value) {
        return redisTemplate.opsForZSet().score(key, value);
    }

    /**
     * ZSet获取value在集合中排名
     */
    public static Long rank(String key, String value) {
        return redisTemplate.opsForZSet().rank(key, value);
    }

    /**
     * ZSet返回有序的集合中，score大的在前面
     */
    public static Set<String> revRange(String key, int start, int end) {
        return redisTemplate.opsForZSet().reverseRange(key, start, end);
    }

    /**
     * 删除ZSet集合元素
     */
    public static void deleteZSet(String key){
        redisTemplate.opsForZSet().remove(key);
    }

    /**
     * Map集合写入缓存
     */
    public static boolean setMap(final String key, Map map) {
        boolean result = false;
        try {
            redisTemplate.opsForHash().putAll(key, map);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * Map集合写入缓存,带过期时间
     */
    public static boolean setMap(final String key, Map map, Long expireTime) {
        boolean result = false;
        try {
            redisTemplate.opsForHash().putAll(key, map);
            redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 取出Map集合
     */
    public static Map getMap(final String key) {
        Map result = null;
        result = redisTemplate.opsForHash().entries(key);
        return result;
    }

    /**
     * 获取自增ID
     */
    public static Long incr(String key){
        RedisAtomicLong redisAtomicLong = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        Long  increment  = redisAtomicLong.getAndIncrement();
//        if ((null == increment || increment.longValue() == 0) && liveTime > 0) {//初始设置过期时间
//            redisAtomicLong.expire(liveTime, TimeUnit.SECONDS);
//        }
        return increment;
    }

    /**
     * 缓存设置
     *
     * @param redisTemplate
     */
    public static void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }
}
